﻿using Common.Entities;
using Common.Exceptions;
using Common.Utils.Constants;
using DataAccess.Repository.Interfaces;
using Infrastructure.Interfaces;
using Infrastructure.Logging;

namespace Infrastructure.Services
{
    public class UserRegistrationProvider : IUserRegistrationProvider
    {
        private readonly IUserRepository _userRepository;
        private readonly ILoggingService _loggingService;
        private readonly IUserEncryptor _userEncryptor;

        public UserRegistrationProvider(IUserRepository userRepository, ILoggingService loggingService, IUserEncryptor userEncryptor)
        {
            _userRepository = userRepository;
            _loggingService = loggingService;
            _userEncryptor = userEncryptor;
        }

        public bool Registrate(User newUser)
        {
            bool savedSuccessfull = false;
            var encryptedUser = _userEncryptor.SecureUser(newUser);
            try
            {
                _userRepository.Save(encryptedUser);
                savedSuccessfull = true;
                _loggingService.ReportInfo(string.Format(InfoMessage.UserRegistratedSuccessfully, newUser.UserName));
            }
            catch (DataAccessException exception)
            {
                _loggingService.ReportError(exception.Message, exception);
            }

            return savedSuccessfull;
        }
    }
}
